package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneList;

import java.io.Serializable;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.vo.Users;

public class UserSearch implements Serializable{
	private static final long serialVersionUID = -4939179697638212411L;
	private CayenneList<Users> userList;

	public UserSearch() {
		userList = new CayenneList<Users>(null);
		List<Users> tempList = new UsersDAO(EduShareSession.get()
				.getSessionORMInterface()).getAllUsers();
		userList.add(tempList);
	}

	public String getUserList(String q) {
		String ret = "";
		q = q.toLowerCase();
		List<IModel<Users>> ul = userList.getObject();
		for (IModel<Users> u : ul) {
			if ((u.getObject().getEmail() != null && u.getObject().getEmail()
					.toLowerCase().contains(q))
					|| u.getObject().getCompleteUserName().toLowerCase()
							.contains(q)
					|| u.getObject().getUserName().toLowerCase().contains(q)) {
				ret += u.getObject().getCompleteUserName() + "|"
						+ u.getObject().getEmail() + "|"
						+ u.getObject().getUserName() + ";";
			}
		}
		return ret;
	}
}
